package team.knowers.qzh.qzhgrame.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import team.knowers.qzh.qzhgrame.common.convention.result.Result;
import team.knowers.qzh.qzhgrame.common.convention.result.Results;
import team.knowers.qzh.qzhgrame.dao.entity.SectionDO;
import team.knowers.qzh.qzhgrame.dto.req.*;
import team.knowers.qzh.qzhgrame.dto.resp.AllSectionRespDTO;
import team.knowers.qzh.qzhgrame.dto.resp.SectionDetailsRespDTO;
import team.knowers.qzh.qzhgrame.dto.resp.SectionPageRespDTO;
import team.knowers.qzh.qzhgrame.service.SectionGrameService;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class SectionGrameController {
    private final SectionGrameService sectionGrameService;

    /**
     * 分页查询作者创作版本
     */
    @PostMapping("/api/qzh-server/admin/v1/grame/section/show")
    public Result<IPage<SectionPageRespDTO>> pageSection(@RequestBody PageSectionReqDTO pageSectionReqDTO) {
        return Results.success(sectionGrameService.pageSection(pageSectionReqDTO));
    }

    /**
     * 创建章节
     */
    @PostMapping("/api/qzh-server/admin/v1/section/add")
    public Result<Integer> addSection(@RequestBody AddSectionReqDTO req) {
        return Results.success(sectionGrameService.addSection(req));
    }

    /**
     * 创建版本
     */
    @PostMapping("/api/qzh-server/admin/v1/section/version")
    public Result<Integer> addVersion(@RequestBody AddVersionReqDTO req) {
        return Results.success(sectionGrameService.addVersion(req));
    }

    /**
     * 删除章节/版本
     */
    @PostMapping("/api/qzh-server/admin/v1/section/delete/{id}")
    public Result<Integer> deleteSection(@PathVariable("id") Long id) {
        return Results.success(sectionGrameService.deleteSection(id));
    }

    /**
     * 展示该单元用户所创作的所有章节
     */
    @GetMapping("/api/qzh-server/admin/v1/grame/section/allshow")
    public Result<List<AllSectionRespDTO>> showSection(@RequestParam("id") Long id, @RequestParam("authorId") Long authorId) {
        return Results.success(sectionGrameService.showSection(id, authorId));
    }

    /**
     * 展示某人创作所有版本
     */
    @GetMapping("/api/qzh-server/admin/v1/grame/section/version/{id}")
    public Result<List<SectionDO>> listOnesVersion(@PathVariable("id") Long id) {
        return Results.success(sectionGrameService.listOnesVersion(id));
    }

    /**
     * 展示某单元所有目录
     */
    @GetMapping("/api/qzh-server/admin/v1/grame/section/content/{id}")
    public Result<List<SectionDetailsRespDTO>> getSectionDetailsById(@PathVariable("id") Long chapterId) {
        return Results.success(sectionGrameService.getSectionDetailsById(chapterId));
    }
}
